package com.yantong.quartz.job;

import com.yantong.pojo.Centre;
import com.yantong.service.CentreService;
import com.yantong.constant.BalanceLogFlowType;
import com.yantong.constant.BalanceLogStyle;
import com.yantong.constant.RechargeType;
import com.yantong.utils.DateUtils;
import com.yantong.pojo.Balance;
import com.yantong.pojo.BalanceLog;
import com.yantong.pojo.CommisLogsParams;
import com.yantong.pojo.CommissionLog;
import com.yantong.mapper.BalanceLogMapper;
import com.yantong.mapper.CommissionLogMapper;
import com.yantong.service.BalanceLogService;
import com.yantong.service.CommissionService;
import com.yantong.pojo.CommisConfig;
import com.yantong.pojo.TradeConfig;
import com.yantong.service.ConfigService;
import com.yantong.pojo.Member;
import com.yantong.mapper.MemberMapper;
import com.yantong.service.MemberService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liaozhaogui
 * @description: CommissionTimer
 * @date 2020/9/26 23:46
 */
@Service
public class CommissionTimer {
	private static Logger logger = LoggerFactory.getLogger(OrderTimer.class);

	private boolean taskOn = true;

	@Autowired
	private ConfigService configService;
	@Autowired
	private CommissionService commissionService;
	@Autowired
	private CommissionLogMapper commissionLogMapper;
	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private BalanceLogMapper balanceLogMapper;
	@Autowired
	private CentreService centreService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private BalanceLogService balanceLogService;



	/**
	 * 分佣定时任务
	 *
	 * @param
	 * @return void
	 * @author huangshuai
	 * @date 2021/1/22 18:49
	 */
	@Scheduled(cron = "0/59 * *  * * ?")
	public void zbUseragentCommissionRelease() {
		if (!taskOn) {
			logger.info("system task set off!autoDelivery!");
			return;
		}
		List<CommissionLog> UserAgentCommissionList = commissionLogMapper.selectByUserAgentCommission();
		if(UserAgentCommissionList!=null && !UserAgentCommissionList.isEmpty()) {
			for (CommissionLog commissionLog : UserAgentCommissionList) {
				//查询需要存入代理佣金的用户
				Member member = memberService.getByKey(commissionLog.getMid());
				//获取配置用户代理计算规则
				CommisConfig useragentCountRuleTypeConfig = configService.getCommissionConfigByCentreId(commissionLog.getCentreId());
				if (useragentCountRuleTypeConfig.getUseragentCountByType() != null) {
					if (0 == useragentCountRuleTypeConfig.getUseragentCountByType()) { //立即到账
						if (member != null) {
							member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
							memberMapper.updateCredit2ByMid(member);
						}
						commissionLogUpdate(commissionLog);
					} else if (1 == useragentCountRuleTypeConfig.getUseragentCountByType()) {//按月结算
						SimpleDateFormat sdf = new SimpleDateFormat("dd HH:mm");
						String date = "25 00:01";
						String nowDate = sdf.format(new Date());
						if (date.equals(nowDate)) {
							if (member != null) {
								member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
								memberMapper.updateCredit2ByMid(member);
							}
							commissionLogUpdate(commissionLog);
						}

					} else if (2 == useragentCountRuleTypeConfig.getUseragentCountByType()) {//按天结算
						SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
						String date = "00:01";
						String nowDate = sdf.format(new Date());
						if (date.equals(nowDate)) {

							if (member != null) {
								member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
								memberMapper.updateCredit2ByMid(member);
							}
							commissionLogUpdate(commissionLog);
						}
					} else if (3 == useragentCountRuleTypeConfig.getUseragentCountByType()) { //按周结算
						JudgeTheWeek(useragentCountRuleTypeConfig.getUseragentWeekType(), member, commissionLog);
					}
				} else {
					if (member != null) {
						member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
						memberMapper.updateCredit2ByMid(member);
					}
					commissionLogUpdate(commissionLog);
				}
				//增加余额变动记录
				BalanceLog supplyBalanceLog = new BalanceLog();
				supplyBalanceLog.setMid(member.getId());
				supplyBalanceLog.setMobile(member.getMobile());
				supplyBalanceLog.setRealname(member.getRealname());
				supplyBalanceLog.setStatus(0);
				supplyBalanceLog.setType(0);
				supplyBalanceLog.setStyle(BalanceLogStyle.SYS_COUNT.getCode());
				supplyBalanceLog.setCredittype("credit2");
				supplyBalanceLog.setBeforeamount(member.getCredit2());
				supplyBalanceLog.setMoney(commissionLog.getCommission());
				supplyBalanceLog.setAfteramount(member.getCredit2().add(commissionLog.getCommission()));
				supplyBalanceLog.setCreateTime(new Date());
				supplyBalanceLog.setCentreId(member.getCentreId());
				supplyBalanceLog.setTitle("拼团成功后结算余额");
				supplyBalanceLog.setRemark("用户使用余额购买大礼包拼团产品，拼团成功后结算余额");
				supplyBalanceLog.setSource(0);
				supplyBalanceLog.setFlowtype(BalanceLogFlowType.TEAM_SETTLEMENT.getCode());
				balanceLogService.save(supplyBalanceLog);
			}
		}

	}

	/**
	 * TODO 如果选择的按周来分发代理佣金
	 *
	 * @param useragentWeekType, member, commissionLog
	 * @return void
	 * @author huangshuai
	 * @date 2021/1/26 14:45
	 */
	private void JudgeTheWeek(Integer useragentWeekType,Member member,CommissionLog commissionLog){
		Calendar cal = Calendar.getInstance();
		int data=cal.get(Calendar.DAY_OF_WEEK);

		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		String date = "00:01";
		String nowDate = sdf.format(new Date());

       if(useragentWeekType == 1){ //周一
	       if((data==Calendar.MONDAY)&&(date.equals(nowDate))){
		       if(member != null) {
			       member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
			       memberMapper.updateCredit2ByMid(member);
		       }
		       commissionLogUpdate(commissionLog);
	       }
       }else if(useragentWeekType == 2){ //周二
	       if ((data==Calendar.TUESDAY)&&(date.equals(nowDate))){
		       if(member != null) {
			       member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
			       memberMapper.updateCredit2ByMid(member);
		       }
		       commissionLogUpdate(commissionLog);
	       }
       }else if(useragentWeekType == 3){ //周三
	       if ((data==Calendar.WEDNESDAY)&&(date.equals(nowDate))){
		       if(member != null) {
			       member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
			       memberMapper.updateCredit2ByMid(member);
		       }
		       commissionLogUpdate(commissionLog);
	       }
       }else if(useragentWeekType == 4){  //周四
	       if ((data==Calendar.THURSDAY)&&(date.equals(nowDate))){
		       if(member != null) {
			       member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
			       memberMapper.updateCredit2ByMid(member);
		       }
		       commissionLogUpdate(commissionLog);
	       }
       }else if(useragentWeekType == 5){ //周五
	       if ((data==Calendar.FRIDAY)&&(date.equals(nowDate))){
		       if(member != null) {
			       member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
			       memberMapper.updateCredit2ByMid(member);
		       }
		       commissionLogUpdate(commissionLog);
	       }
       }else if(useragentWeekType == 6){ //周六
	       if ((data==Calendar.SATURDAY)&&(date.equals(nowDate))){
		       if(member != null) {
			       member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
			       memberMapper.updateCredit2ByMid(member);
		       }
		       commissionLogUpdate(commissionLog);
	       }
       }else if(useragentWeekType == 7){  //周天
	       if ((data==Calendar.SUNDAY)&&(date.equals(nowDate))){
		       if(member != null) {
			       member.setCredit2(member.getCredit2().add(commissionLog.getCommission()));//代理佣金存入用户余额
			       memberMapper.updateCredit2ByMid(member);
		       }
		       commissionLogUpdate(commissionLog);
	       }
       }
	}

	/**
	 * 更改佣金记录的状态
	 */
	private int commissionLogUpdate(CommissionLog log) {
		log.setGrantTime(new Date());
		log.setIsGrant(1);
		return commissionLogMapper.updateByPrimaryKeySelective(log);
	}

}
